package com.qeeyou.apps.android.craftsman.utils;

import android.content.Context;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.qeeyou.apps.android.craftsman.R;

import java.io.File;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.CropSquareTransformation;

public class ImageLoader {

    private static ImageLoader instance;

    public ImageLoader() {

    }

    public static synchronized ImageLoader getInstance() {
        if (instance == null) {
            instance = new ImageLoader();
        }
        return instance;
    }

    public String getImageUrl(String partUrl) {
        return "http://39.104.58.100:8080/InstallIt" + partUrl;
    }

    public void displayImage(Context context, String url, ImageView view) {
        Glide.with(context)
                .load(url)
                .crossFade()
                .into(view);
    }

    public void displayImageById(Context context, int imageRes, ImageView view) {
        Glide.with(context)
                .load(imageRes)
                .crossFade()
                .into(view);
    }

    public void displayImageByFile(Context context, String filePath, ImageView view) {
        File file = new File(filePath);
        Glide.with(context)
                .load(file)
                .crossFade()
                .into(view);
    }

    public void displayHeaderPic(Context context, String url, ImageView view) {
        Glide.with(context)
                .load(url)
                .placeholder(R.drawable.ic_message_avatar) // can also be a drawable
                .error(R.drawable.ic_message_avatar) // will be displayed if the image cannot be loaded
                .bitmapTransform(new CropCircleTransformation(context))
                .crossFade()
                .into(view);
    }

    public void displayListHeaderPic(Context context, String url, ImageView view) {
        Glide.with(context)
                .load(url)
                .placeholder(R.drawable.ic_master_avatar) // can also be a drawable
                .error(R.drawable.ic_master_avatar) // will be displayed if the image cannot be loaded
                .bitmapTransform(new CropSquareTransformation(context))
                .crossFade()
                .into(view);
    }
//
//    public void displayDoctorGroupHeaderPic(Context context, String url, ImageView view) {
//        Glide.with(context)
//                .load(url)
//                .placeholder(R.drawable.moren_zu_icon) // can also be a drawable
//                .error(R.drawable.moren_zu_icon) // will be displayed if the image cannot be loaded
//                .bitmapTransform(new CropCircleTransformation(context))
//                .crossFade()
//                .into(view);
//    }
//
//    public void displayCustomImage(Context context, String url, ImageView view) {
//        if (StringUtils.isNotEmpty(url)) {
//            Glide.with(context)
//                    .load(url)
//                    .placeholder(R.drawable.moren_tupian_icon) // can also be a drawable
//                    .error(R.drawable.tupian_jiazaishibai_icon) // will be displayed if the image cannot be loaded
//                    .crossFade()
//                    .into(view);
//        } else {
//            Glide.with(context)
//                    .load(R.drawable.moren_tupian_icon)
//                    .crossFade()
//                    .into(view);
//        }
//    }
//
//    public void displayCustomArticle(Context context, String url, ImageView view) {
//        if (StringUtils.isNotEmpty(url)) {
//            Glide.with(context)
//                    .load(url)
//                    .placeholder(R.drawable.moren_wenzhang_icon_44) // can also be a drawable
//                    .error(R.drawable.tupian_jiazaishibai_icon) // will be displayed if the image cannot be loaded
//                    .crossFade()
//                    .into(view);
//        } else {
//            Glide.with(context)
//                    .load(R.drawable.moren_wenzhang_icon_44)
//                    .crossFade()
//                    .into(view);
//        }
//    }
//
//    public void displayCustomImage(Context context, String url, Target<Bitmap> target) {
//        Glide.with(context)
//                .load(url)
//                .asBitmap()
//                .placeholder(R.drawable.moren_tupian_icon) // can also be a drawable
//                .error(R.drawable.tupian_jiazaishibai_icon) // will be displayed if the image cannot be loaded
//                .into(target);
//    }
//
//    public void displayDepartGroup(Context context, String url, ImageView view, int type) {
//        Glide.with(context)
//                .load(url)
//                .placeholder(type == 1 ? R.drawable.lxr_keshi_icon : R.drawable.lxr_zutouxiang_icon) // can also be a drawable
//                .error(type == 1 ? R.drawable.lxr_keshi_icon : R.drawable.lxr_zutouxiang_icon) // will be displayed if the image cannot be loaded
//                .bitmapTransform(new CropCircleTransformation(context))
//                .crossFade()
//                .into(view);
//    }
//
//    public void displayReceiver(Context context, String url, ImageView view, int type) {
//        Glide.with(context)
//                .load(url)
//                .placeholder(type == 2 ? R.drawable.lxr_zutouxiang_icon : R.drawable.moren_touxiang_xiao) // can also be a drawable
//                .error(type == 2 ? R.drawable.lxr_zutouxiang_icon : R.drawable.moren_touxiang_xiao) // will be displayed if the image cannot be loaded
//                .bitmapTransform(new CropCircleTransformation(context))
//                .crossFade()
//                .into(view);
//    }

    public void clear(Context context) {
        Glide.get(context).clearMemory();
    }
}